﻿#include "BrowserTableHandlerMaterialAbstract.h"
#include "WidgetBrowserTable.h"
#include "OperatorsInterface/OperAbstractMaterial.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include <QMenu>
#include <QHeaderView>

namespace GUI
{

    BrowserTableHandlerMaterialAbstract::~BrowserTableHandlerMaterialAbstract()
    {
    }

    void BrowserTableHandlerMaterialAbstract::contextMenu(QMenu* popMenu)
    {
        if (popMenu == nullptr || _tableWidget == nullptr) return;
      
       //添加按钮
        QAction* act = this->AddMenuAction(popMenu, tr("Edit"));
        connect(act, SIGNAL(triggered()), this, SLOT(editMaterialSlot()));
        //选择一个的时候，不可以批量可以设置属性
        act = this->AddMenuAction(popMenu, tr("Set Attribute"));
        act->setEnabled(this->enableAttriSet());
        connect(act, SIGNAL(triggered()), this, SLOT(setAttributeSlot()));

        act = this->AddMenuAction(popMenu, tr("Duplicate"));
        connect(act, SIGNAL(triggered()), this, SLOT(duplicateMaterialSlot()));
        act = this->AddMenuAction(popMenu, tr("Create"));
        connect(act, SIGNAL(triggered()), this, SLOT(createMaterialSlot()));
        act = this->AddMenuAction(popMenu, tr("Copy as Text"));
        connect(act, SIGNAL(triggered()), this, SLOT(copyAsTextSlot()));
        popMenu->addSeparator();
        act = this->AddMenuAction(popMenu, tr("Delete"));
        connect(act, SIGNAL(triggered()), this, SLOT(removeMaterialSlot()));
    }


    void BrowserTableHandlerMaterialAbstract::headerContextMenu(QMenu* popMenu, int coluIndex)
    {
        if (popMenu == nullptr || _tableWidget == nullptr || coluIndex < 0) return;


    }

    void BrowserTableHandlerMaterialAbstract::itemDoubleClicked(int row, int colum)
    {
        //双击编辑操作
        if (_tableWidget == nullptr) return;
        this->editMaterialSlot();
    }

    EventOper::OperAbstractMaterial* BrowserTableHandlerMaterialAbstract::getMaterialOper()
    {
        //纯虚函数
        return nullptr;
    }

    bool BrowserTableHandlerMaterialAbstract::enableAttriSet()
    {
        return false;
    }

    void BrowserTableHandlerMaterialAbstract::editMaterialSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractMaterial* oper = this->getMaterialOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
         //操作器调用
        oper->editMaterial(matID);
    }

    void BrowserTableHandlerMaterialAbstract::createMaterialSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractMaterial* oper = this->getMaterialOper();
        if (oper == nullptr) return;
         //操作器调用
        oper->createMaterial();
    }

    void BrowserTableHandlerMaterialAbstract::duplicateMaterialSlot()
    {
        if (_tableWidget == nullptr) return;
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractMaterial* oper = this->getMaterialOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
         //操作器调用
        oper->duplicateMaterial(matID);
    }

    void BrowserTableHandlerMaterialAbstract::removeMaterialSlot()
    {
        if (_tableWidget == nullptr) return;
        if (_tableWidget->rowCount()<=1) {
            AppFrame::FITKMessageError("Please keep at least one");
            return;
        }
        QTableWidgetItem* selectItem = this->getSelectItem();
        if (selectItem == nullptr) return;
        EventOper::OperAbstractMaterial* oper = this->getMaterialOper();
        if (oper == nullptr) return;
        const int matID = selectItem->type();
        if (matID < 1) return;
         //操作器调用
        oper->removeMaterial(matID);
    }

    void BrowserTableHandlerMaterialAbstract::setAttributeSlot()
    {
        if (_tableWidget == nullptr) return;
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2)return;
        EventOper::OperAbstractMaterial* oper = this->getMaterialOper();
        if (oper == nullptr) return;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return;
        //名称列
        const int colIndex = item->column();
        QTableWidgetItem* hitem = _tableWidget->horizontalHeaderItem(colIndex);
        if (hitem == nullptr) return;
        const QString text = hitem->text();
        QList<int> itemID;
        //选取的对象根据第一个对象判断
        for (QTableWidgetItem* item : selectItems)
        {
            if (item == nullptr) continue;
            int col = item->column();
            if (col == colIndex)
                //记录对象ID
                itemID.append(item->type());
        }
        //执行批量修改
        oper->setAttribute(text, item->text() , colIndex, itemID);

    }

 
}



